// src/utils/map.ts - 纯前端地图工具
// 接口定义已移除
  
  // 定义状态颜色映射
  const statusColors = {
    free: '#52c41a',
    busy: '#fa8c16', 
    maintenance: '#f5222d'
  } as const
  
  type StationStatus = keyof typeof statusColors
  
  // 简化版地图加载 - 如果高德地图加载失败，使用备用方案
  export const loadAMap = (): Promise<any> => {
    return new Promise((resolve) => {
      // 检查是否已加载
      if (typeof window !== 'undefined' && (window as any).AMap) {
        resolve((window as any).AMap)
        return
      }
      
      // 备用方案：如果高德地图加载失败，返回一个模拟的地图对象
      const fallbackMap = {
        Map: class {
          constructor(_container: string, _options: any) {
            console.log('使用备用地图方案')
          }
          addControl() {}
          add() {}
          setZoom() {}
          setCenter() {}
        },
        Marker: class {},
        MarkerCluster: class {}
      }
      
      // 尝试加载高德地图，失败时使用备用方案
      const script = document.createElement('script')
      script.src = `https://webapi.amap.com/maps?v=1.4.15&key=你的高德地图KEY&plugin=AMap.MarkerClusterer`
      script.onload = () => {
        if ((window as any).AMap) {
          resolve((window as any).AMap)
        } else {
          console.warn('高德地图加载失败，使用备用方案')
          resolve(fallbackMap)
        }
      }
      script.onerror = () => {
        console.warn('高德地图加载失败，使用备用方案')
        resolve(fallbackMap)
      }
      document.head.appendChild(script)
    })
  }
  
  // 创建标记内容
  export const createMarkerContent = (station: { status: StationStatus; availableCount: number; totalCount: number }) => {
    const statusColor = statusColors[station.status]
  
    return `
      <div class="custom-marker" style="
        width: 40px; height: 40px; 
        background: ${statusColor};
        border: 3px solid white;
        border-radius: 50%;
        color: white;
        font-size: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 6px rgba(0,0,0,0.3);
        font-weight: bold;
        cursor: pointer;
      ">
        ${station.availableCount}/${station.totalCount}
      </div>
    `
  }
  
  // 模拟地图服务
  export const mockMapService = {
    // 模拟路径规划
    calculateRoute: async (_from: [number, number], _to: [number, number]) => {
      await new Promise(resolve => setTimeout(resolve, 1000))
      return {
        distance: Math.round(Math.random() * 10000) / 10, // 公里
        duration: Math.round(Math.random() * 120), // 分钟
        points: [] // 简化版不需要真实路径点
      }
    },
    
    // 模拟逆地理编码
    reverseGeocode: async (lng: number, lat: number) => {
      await new Promise(resolve => setTimeout(resolve, 500))
      return `模拟地址: ${lng.toFixed(4)}, ${lat.toFixed(4)}`
    }
  }